<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <title>病人信息</title>
    <div th:insert="user/common::head"></div>
</head>

<body class="">
<div class="wrapper ">
    <div th:insert="~{user/common::sidebar(active='patient_list.html')}"></div>
    <div class="main-panel">
        <!-- Navbar导航栏 -->
        <nav th:replace="~{user/common::navbar}"></nav>
        <!-- End Navbar -->
        <div class="panel-header panel-header-sm">
        </div>
        <div class="content">
            <div class="row">
                <div class="col-md-8 offset-2">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="title">病人信息</h5>
                        </div>
                        <div class="card-body">
                            <form id="update_form" method="post">
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <input name="patientId" type="text" hidden="hidden" th:value="${patient.getPatientId()}">
                                            <input name="inDate" type="date" hidden="" th:value="${#dates.format(patient.getInDate(),'yyyy-MM-dd')}">
                                            <label>病人姓名</label>
                                            <input name="patientName" type="text" class="form-control" placeholder="请输入病人的姓名" th:value="${patient.getPatientName()}" readonly>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>联系方式</label>
                                            <input name="patientPhone" type="number" class="form-control" placeholder="病人的手机号" th:value="${patient.getPatientPhone()}">
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>病人性别</label>
                                            <select name="patientGender" class="form-control" placeholder="选择患者性别..." th:value="${patient.getPatientGender()}">
                                                <option value="1">男</option>
                                                <option value="0">女</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>病人年龄</label>
                                            <input name="patientAge" type="number" class="form-control" placeholder="患者年龄" th:value="${patient.getPatientAge()}">
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>主要使用药物</label>
                                            <select name="drugId" type="text" class="form-control" placeholder="请选择主要使用药物">
                                                <option th:each="drug,index:${drugs}" th:text="${drug.getDrugName()}" th:value="${drug.getDrugId()}" th:selected="${drug.getDrugId()==patient.getDrugId()?'seleted':''}"></option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>出院日期</label>
                                            <input name="outDate" type="date" class="form-control" placeholder="Home Address" onclick="getDate()" min="" th:value="${#dates.format(patient.getOutDate(),'yyyy-MM-dd')}">
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-4 pr-1">
                                        <div class="form-group">
                                            <label>选择科室</label>
                                            <select name="departmentId" id="departmentId" type="text" class="form-control" placeholder="选择科室" onchange="inpatientSelect()">
                                                <option selected value="0">请选择科室...</option>
                                                <option th:each="department,index:${departments}"
                                                        th:text="${department.getDepartmentName()}"
                                                        th:value="${department.getDepartmentId()}"</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-4 px-1">
                                        <div class="form-group">
                                            <label>选择病房</label>
                                            <select name="inpatientwardId" type="text" onchange="bedSelect()" id="inpatientwardId" class="form-control" placeholder="选择病房">
                                                <option selected value="0">请选择病房...</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-4 px-1">
                                        <div class="form-group">
                                            <label>选择病床</label>
                                            <select name="bedId" type="text" id="bedId" class="form-control" placeholder="选择病床">
                                                <option selected value="0">请选择床位...</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-6 offset-3">
                                        <div class="form-group">
                                            <label>主治医师</label>
                                            <select name="doctorId" id="doctorId" type="number" class="form-control" placeholder="选择医生">
                                                <option selected value="0">请选择医生...</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>


                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label>诊断结果</label>
                                            <textarea name="patientResult" rows="5" cols="80" class="form-control" placeholder="请输入患者的诊断结果。" style="max-height: 200px" th:text="${patient.getPatientResult()}"></textarea>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-8 offset-3">
                                       <a class="btn-warning btn btn-lg col-4 text-white" th:href="@{/toPatientList}">返回</a>
                                        <input type="button" onclick="updateInfo()" class="btn-primary btn btn-lg col-4" value="修改">
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--底部footer-->
        <footer th:replace="~{user/common::footer}"></footer>
    </div>
</div>


<script>
    window.onload = function init(){
        var departmentId = [[${patient.getDepartmentId}]];
        $("#departmentId").val(departmentId);
        var inpatientwardId = [[${patient.getInpatientwardId()}]]
        $("#inpatientwardId").val(inpatientwardId);
        inpatientSelect();
        bedSelect();



    }


    function getDate(){
        //得到当前时间
        var date_now = new Date();
        //得到当前年份
        var year = date_now.getFullYear();
        //得到当前月份
        //  1：js中获取Date中的month时，会比当前月份少一个月，所以这里需要先加一
        //  2: 判断当前月份是否小于10，如果小于，那么就在月份的前面加一个 '0' ， 如果大于，就显示当前月份
        var month = date_now.getMonth()+1 < 10 ? "0"+(date_now.getMonth()+1) : (date_now.getMonth()+1);
        //得到当前日子（多少号）
        var date = date_now.getDate() < 10 ? "0"+date_now.getDate() : date_now.getDate();
        date = date + 1; //至少住一天院
        //设置input标签的min属性
        $("input[name='outDate']").attr("min",year+"-"+month+"-"+date);
    }

    /**
     * 点击选择科室的时 查询当前选择科室的所有医生与可用房间
     */
    function inpatientSelect(){
        var departmentId = $("#departmentId").val();
        var inpatientWardSelector = $("#inpatientwardId");
        var doctorSelector = $("#doctorId");


        $.ajax({
            sync:false,
            url:'/inpatientSelect',
            data:{
                'DepartmentId':departmentId,
            },
            method:'get',
            dataType:'json',
            success:function(result){
                inpatientWardSelector.empty();
                $("#bedId").empty();
                if(result.length===0){
                    inpatientWardSelector.append('<option value='+0+'>该科室暂无病房可用！</option>'); //为Select追加一个Option(下拉项)
                    return;
                }
                result.forEach(function (item){
                    inpatientWardSelector.append('<option value='+item.inpatientwardId+'>'+item.inpatientwardId+'</option>'); //为Select追加一个Option(下拉项)
                });
                bedSelect();

            }
        })

        $.ajax({
            sync:false,
            url:'/doctorSelect',
            data:{
                'DepartmentId':departmentId,
            },
            method:'get',
            dataType:'json',
            success:function(result){
                doctorSelector.empty();
                if(result.length===0){
                    doctorSelector.append('<option value='+0+'>该科室暂无空闲的医生！</option>');
                    return;
                }
                result.forEach(function (item){
                    console.log(item.doctorId);
                    doctorSelector.append('<option value='+item.doctorId+'>'+item.doctorName+'</option>'); //为Select追加一个Option(下拉项)
                });

            }
        })


    }

    /*
    * 点击选择病房时 查询当前选择的病房里所有可用的(state=0的病床
    * */
    function bedSelect(){
        var inpatientwardId = $("#inpatientwardId").val();
        var bedSelector = $("#bedId");

        $.ajax({
            sync:false,
            url:'/queryAvailableBedsByInpatientWardId',
            data:{
                'inpatientWardId':inpatientwardId,
            },
            method:'get',
            dataType:'json',
            success:function(result){

                bedSelector.empty();
                if(result.length===0){
                    bedSelector.append('<option value='+0+'>该病房暂无可用的床位！</option>');
                    return;
                }
                result.forEach(function (item){
                    console.log(item.bedId);
                    bedSelector.append('<option value='+item.bedId+'>'+item.bedId+'号床</option>'); //为Select追加一个Option(下拉项)
                });

            }
        })

    }


    /**
     * 病人修改提交
     */
    function updateInfo(){
        var formData = $("#update_form").serialize();
        $.ajax({
            sync:false,
            url:'/patient/update',
            data: formData,
            method:'post',
            dataType:'text',
            success:function(result){
                if(result==='修改成功!'){
                    demo.showNotification('success',result);
                    return;
                }
                demo.showNotification('danger',result);
            }
        })

    }

</script>

<!--通用footerJs-->
<script th:replace="~{user/common::footerJs}"></script>

</body>
</html>
